package com.admin.boot.modules.system.controller;

import com.admin.boot.modules.system.entity.SysRole;
import com.admin.boot.modules.system.service.ISysRoleService;

import com.admin.boot.common.utils.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @version v3.0
* @Description: TODO
* @author: admin-boot
* @date: 2024-01-15
*/
@Api(tags = "角色表")
@RestController
@RequestMapping("/sysRole/sysRole")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @GetMapping("/list")
    @ApiOperation(value = "角色表-分页列表查询", notes = "角色表-分页列表查询")
    public Result<?> list(SysRole sysRole,
                          @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        Page<SysRole> page = new Page<>(pageNo, pageSize);
        Page<SysRole> sysRolePage = sysRoleService.page(page, queryWrapper);
        return Result.OK(sysRolePage);
    }

    @GetMapping("/queryList")
    @ApiOperation(value = "用户表-分页列表查询", notes = "用户表-分页列表查询")
    public Result<?> queryList(SysRole sysRole) {
        List<SysRole> list = sysRoleService.list(null);
        return Result.OK(list);
    }

    @PostMapping("/add")
    @ApiOperation(value = "角色表-添加", notes = "角色表-添加")
    public Result<?> add(@RequestBody SysRole sysRole) {
        String roleName = sysRole.getRoleName();
        String code = sysRole.getCode();
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRole::getRoleName,roleName);
        List<SysRole> list = sysRoleService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(list)){
            return Result.error("角色名重复！");
        }
        LambdaQueryWrapper<SysRole> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRole::getCode,code);
        List<SysRole> list1 = sysRoleService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(list1)){
            return Result.error("角色编码重复！");
        }
        sysRoleService.save(sysRole);
        return Result.OK("添加成功！");
    }


    @PostMapping("/edit")
    @ApiOperation(value = "角色表-编辑", notes = "角色表-编辑")
    public Result<?> edit(@RequestBody SysRole sysRole) {
        sysRoleService.updateById(sysRole);
        return Result.OK("编辑成功！");
    }

    @GetMapping("/deleteById")
    @ApiOperation(value = "角色表-删除", notes = "角色表-删除")
    public Result<?> deleteById(String id) {
        sysRoleService.removeById(id);
        return Result.OK("删除成功！");
    }

    @GetMapping("/deleteBatch")
    @ApiOperation(value = "角色表-批量删除", notes = "角色表-批量删除")
    public Result<?> deleteBatch(String ids) {
        for (String id : ids.split(",")) {
            sysRoleService.removeById(id);
        }
        return Result.OK("删除成功！");
    }

}
